package com.time.timespace.controller;

import com.github.pagehelper.PageInfo;
import com.time.timespace.commons.dto.PageRequest;
import com.time.timespace.commons.dto.ResponseEntity;
import com.time.timespace.domain.dto.CategoryCountDTO;
import com.time.timespace.domain.dto.ChargeDTO;
import com.time.timespace.domain.entity.Charge;
import com.time.timespace.service.ChargeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Tag(name = "支出管理")
@Slf4j
@RequestMapping("/charge")
public class ChargeController {

    private final ChargeService chargeService;

    public ChargeController(ChargeService chargeService) {
        this.chargeService = chargeService;
    }

    @Operation(description = "查询当前用户支出信息")
    @GetMapping("/select-self")
    public ResponseEntity<PageInfo<Charge>> selectArticle(PageRequest pageRequest, Long createdBy) {
        return ResponseEntity.success(chargeService.selectChargeById(pageRequest, createdBy));
    }

    @Operation(description = "查询支出信息")
    @GetMapping("/select-charge")
    public ResponseEntity<PageInfo<Charge>> selectCharge(PageRequest pageRequest, Charge charge) {
        return ResponseEntity.success(chargeService.selectCharge(pageRequest, charge));
    }

    @Operation(description = "查询年度全数据")
    @GetMapping("/year-data")
    public ResponseEntity<List<ChargeDTO>> selectYearData(String year, Long userId) {
        return ResponseEntity.success(chargeService.selectYearData(year, userId));
    }

    @Operation(description = "查询支出分类数量金额")
    @GetMapping("/category-count")
    public ResponseEntity<List<CategoryCountDTO>> categoryCount(String year, Long userId) {
        return ResponseEntity.success(chargeService.categoryCount(year, userId));
    }

    @Operation(description = "导出年度分类明细数组")
    @GetMapping("/export")
    public void exportExpense(HttpServletResponse response, String year, Long userId) {
        chargeService.exportExpense(response, year, userId);
    }

    @Operation(description = "保存支出信息")
    @PostMapping("/save-charge")
    public ResponseEntity<Charge> saveCharge(@RequestBody Charge charge) {
        return ResponseEntity.success(chargeService.saveCharge(charge));
    }

    @Operation(description = "删除支出信息")
    @GetMapping("/delete-charge/{chargeId}")
    public ResponseEntity<Void> deleteCharge(@PathVariable("chargeId") Long chargeId) {
        chargeService.deleteCharge(chargeId);
        return ResponseEntity.success();
    }
}
